package com.allways.dubbo.mdm.service.logipoint.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.logipoint.Logipoint;
import com.allways.dubbo.mdm.service.logipoint.ILogipointService;

/**
 * <p>ClassName: ILogipointService</p>
 * <p>Description: 物流运作点Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-09</p>
 */
@Service("mdmLogipointService")
public class LogipointServiceImpl extends MdmBaseDaoImpl implements ILogipointService {
	Log log = LogFactory.getLog(LogipointServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addLogipoint(Logipoint logipoint) {
   		log.debug("For the new logipoint parameters:"+ logipoint.toJson());
   		validColumnLen(logipoint);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(logipoint.getLogipointCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getLogipointByCode(logipoint.getLogipointCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("logipoint.insertLogipoint",logipoint);
    }

    @Override
    public int updateLogipoint(Logipoint logipoint) {
    	log.debug("Update for the logipoint parameters:"+ logipoint.toJson());
    	validColumnLen(logipoint);
     	//无code的不能更新
        if(StringUtils.isEmpty(logipoint.getLogipointCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Logipoint oldLogipoint = getLogipointByCode(logipoint.getLogipointCode());
        if(null == oldLogipoint){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldLogipoint.setPointId(logipoint.getPointId());
		oldLogipoint.setPointCode(logipoint.getPointCode());
		oldLogipoint.setPointName(logipoint.getPointName());
		oldLogipoint.setOrgType(logipoint.getOrgType());
		oldLogipoint.setCompanyCode(logipoint.getCompanyCode());
		oldLogipoint.setDealerCode(logipoint.getDealerCode());
		oldLogipoint.setNationCode(logipoint.getNationCode());
		oldLogipoint.setProvinceCode(logipoint.getProvinceCode());
		oldLogipoint.setCityCode(logipoint.getCityCode());
		oldLogipoint.setDistrictCode(logipoint.getDistrictCode());
		oldLogipoint.setStreet(logipoint.getStreet());
		oldLogipoint.setAddress(logipoint.getAddress());
		oldLogipoint.setPostcode(logipoint.getPostcode());
		oldLogipoint.setContact(logipoint.getContact());
		oldLogipoint.setMobiPhone(logipoint.getMobiPhone());
		oldLogipoint.setTel(logipoint.getTel());
		oldLogipoint.setFax(logipoint.getFax());
		oldLogipoint.setLatitude(logipoint.getLatitude());
		oldLogipoint.setLongitude(logipoint.getLongitude());
		oldLogipoint.setPointType(logipoint.getPointType());
		oldLogipoint.setWhPosition(logipoint.getWhPosition());
		oldLogipoint.setWhSynchronize(logipoint.getWhSynchronize());
		oldLogipoint.setWhNonconsign(logipoint.getWhNonconsign());
		oldLogipoint.setWhParentId(logipoint.getWhParentId());
		oldLogipoint.setWhProperty(logipoint.getWhProperty());
		oldLogipoint.setWhCapacity(logipoint.getWhCapacity());
		oldLogipoint.setWhInnerCapacity(logipoint.getWhInnerCapacity());
		oldLogipoint.setWhOuterCapacity(logipoint.getWhOuterCapacity());
		oldLogipoint.setVhCapacity(logipoint.getVhCapacity());
		oldLogipoint.setMaxVhCapacity(logipoint.getMaxVhCapacity());
		oldLogipoint.setWhPdi(logipoint.getWhPdi());
		oldLogipoint.setWhDriveway(logipoint.getWhDriveway());
		oldLogipoint.setWhCrossdock(logipoint.getWhCrossdock());
		oldLogipoint.setWhOffline(logipoint.getWhOffline());
		oldLogipoint.setWhWms(logipoint.getWhWms());
		oldLogipoint.setWhRealwh(logipoint.getWhRealwh());
		oldLogipoint.setWhStarttime(logipoint.getWhStarttime());
		oldLogipoint.setWhEndtime(logipoint.getWhEndtime());
		oldLogipoint.setWhLegacy(logipoint.getWhLegacy());
		oldLogipoint.setWhArea(logipoint.getWhArea());
		oldLogipoint.setWhEsc(logipoint.getWhEsc());
		oldLogipoint.setTotalStore(logipoint.getTotalStore());
		oldLogipoint.setLeadTime(logipoint.getLeadTime());
		oldLogipoint.setLoadTime(logipoint.getLoadTime());
		oldLogipoint.setStdInterface(logipoint.getStdInterface());
		oldLogipoint.setRecordPhone(logipoint.getRecordPhone());
		oldLogipoint.setNewDriveway(logipoint.getNewDriveway());
		oldLogipoint.setDataSource(logipoint.getDataSource());
		oldLogipoint.setDeleteMark(logipoint.getDeleteMark());
		oldLogipoint.setCreateUser(logipoint.getCreateUser());
		oldLogipoint.setCreateTime(logipoint.getCreateTime());
		oldLogipoint.setUpdateUser(logipoint.getUpdateUser());
		oldLogipoint.setUpdateTime(logipoint.getUpdateTime());
        
        return update("logipoint.updateLogipoint",oldLogipoint);
    }

    @Override
    public int deleteLogipoint(Logipoint logipoint) {
    	log.debug("Delete for the logipoint parameters:"+ logipoint.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(logipoint.getLogipointCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("logipoint.deleteLogipoint",logipoint);
    }

    @Override
    public Logipoint getLogipointByCode(String code) {
    	log.debug("Query for logipoint code:" + code);
        return queryForObject("logipoint.getLogipointByCode",code);
    }
    @Override
    public long getLogipointCount(Logipoint logipoint) {
    	log.debug("Query for logipoint count parameters:" + logipoint.toJson());
        return queryForObject("logipoint.getLogipointCount",logipoint);
    }
   @Override
    public List<Logipoint> queryLogipoints(Logipoint logipoint, int firstRow, int lastRow) {
      	log.debug("Query for logipoint parameters:" + logipoint.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("logipoint",logipoint);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("logipoint.queryLogipoints", param);
    }
    
    /**
     * 验证字段长度
     * @param logipoint 需要验证的对象
     */
    private void validColumnLen(Logipoint logipoint){
		if(!StringUtils.isEmpty(logipoint.getPointId()) && logipoint.getPointId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getPointCode()) && logipoint.getPointCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logipoint.getPointName()) && logipoint.getPointName().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logipoint.getOrgType()) && logipoint.getOrgType().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getCompanyCode()) && logipoint.getCompanyCode().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(logipoint.getDealerCode()) && logipoint.getDealerCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logipoint.getNationCode()) && logipoint.getNationCode().length()>2){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{2},null);
        }
		if(!StringUtils.isEmpty(logipoint.getProvinceCode()) && logipoint.getProvinceCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logipoint.getCityCode()) && logipoint.getCityCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logipoint.getDistrictCode()) && logipoint.getDistrictCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logipoint.getStreet()) && logipoint.getStreet().length()>30){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{30},null);
        }
		if(!StringUtils.isEmpty(logipoint.getAddress()) && logipoint.getAddress().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(logipoint.getPostcode()) && logipoint.getPostcode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logipoint.getContact()) && logipoint.getContact().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(logipoint.getMobiPhone()) && logipoint.getMobiPhone().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(logipoint.getTel()) && logipoint.getTel().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(logipoint.getFax()) && logipoint.getFax().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(logipoint.getLatitude()) && logipoint.getLatitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
		if(!StringUtils.isEmpty(logipoint.getLongitude()) && logipoint.getLongitude().length()>25){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{25},null);
        }
		if(!StringUtils.isEmpty(logipoint.getPointType()) && logipoint.getPointType().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhPosition()) && logipoint.getWhPosition().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhSynchronize()) && logipoint.getWhSynchronize().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhNonconsign()) && logipoint.getWhNonconsign().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhParentId()) && logipoint.getWhParentId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhProperty()) && logipoint.getWhProperty().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhCapacity()) && logipoint.getWhCapacity().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhInnerCapacity()) && logipoint.getWhInnerCapacity().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhOuterCapacity()) && logipoint.getWhOuterCapacity().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getVhCapacity()) && logipoint.getVhCapacity().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getMaxVhCapacity()) && logipoint.getMaxVhCapacity().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhPdi()) && logipoint.getWhPdi().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhDriveway()) && logipoint.getWhDriveway().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhCrossdock()) && logipoint.getWhCrossdock().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhOffline()) && logipoint.getWhOffline().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhWms()) && logipoint.getWhWms().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhRealwh()) && logipoint.getWhRealwh().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhStarttime()) && logipoint.getWhStarttime().length()>5){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{5},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhEndtime()) && logipoint.getWhEndtime().length()>5){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{5},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhLegacy()) && logipoint.getWhLegacy().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhArea()) && logipoint.getWhArea().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getWhEsc()) && logipoint.getWhEsc().length()>200){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{200},null);
        }
		if(!StringUtils.isEmpty(logipoint.getTotalStore()) && logipoint.getTotalStore().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getLeadTime()) && logipoint.getLeadTime().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(logipoint.getLoadTime()) && logipoint.getLoadTime().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(logipoint.getStdInterface()) && logipoint.getStdInterface().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logipoint.getRecordPhone()) && logipoint.getRecordPhone().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(logipoint.getNewDriveway()) && logipoint.getNewDriveway().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(logipoint.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
